package com.wyp168.renrenfilm.core.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyp168.renrenfilm.core.dao.FilmDao;
import com.wyp168.renrenfilm.core.dao.HomeDao;
import com.wyp168.renrenfilm.core.dao.PlayDao;
import com.wyp168.renrenfilm.core.dao.WatchDao;
import com.wyp168.renrenfilm.core.entity.FilmEntity;
import com.wyp168.renrenfilm.core.entity.HomeEntity;
import com.wyp168.renrenfilm.core.entity.PlayEntity;
import com.wyp168.renrenfilm.core.entity.WatchEntity;
import com.wyp168.renrenfilm.core.service.HomeService;
import com.wyp168.renrenfilm.core.vo.HomeReqVO;
import com.wyp168.renrenfilm.core.vo.resp.HomeRespVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

@Service
public class HomeServiceImpl extends ServiceImpl<HomeDao, HomeEntity> implements HomeService{

    @Resource
    HomeDao homeDao;

    @Autowired
    PlayDao playDao;

    @Autowired
    FilmDao filmDao;

    @Autowired
    WatchDao watchDao;

    @Override
    public List<HomeRespVO> list(HomeReqVO homeReqVO) {
        return homeDao.list(homeReqVO);
    }

    @Transactional(rollbackFor = Exception.class, timeout = 3000)
    @Override
    public void saveHomeInfo(HomeEntity homeEntity) {
        //校验播放室名称是否可用
        if (homeDao.selectCount(new QueryWrapper<HomeEntity>().eq("title", homeEntity.getTitle())) > 0) {
            throw new RuntimeException("创建播放室失败，播放室名称重复");
        }
        //更新film表中的playCount字段
        FilmEntity filmEntity = filmDao.selectById(homeEntity.getFilmId());
        filmEntity.setPlayCount(filmEntity.getPlayCount() + 1);
        filmDao.updateById(filmEntity);
        this.save(homeEntity);
//
//        int i = 10 / 0;

        //向播放者表中添加记录
        PlayEntity playEntity = new PlayEntity();
        BeanUtils.copyProperties(homeEntity, playEntity, "id");
        playEntity.setCreatedAt(LocalDateTime.now());
        playEntity.setCreatedBy(homeEntity.getOwnerName());
        playEntity.setModifiedAt(LocalDateTime.now());
        playEntity.setModifiedBy(homeEntity.getOwnerName());

        playDao.insert(playEntity);
    }

    @Override
    public void saveStateAndDeleteHomeById(Integer homeId) {
        //根据homeId获取播放者id
        HomeEntity homeEntity = this.getById(homeId);
        QueryWrapper<PlayEntity> playEntityQueryWrapper = new QueryWrapper<>();
        playEntityQueryWrapper.eq("created_by", homeEntity.getOwnerName());
        playDao.delete(playEntityQueryWrapper);

        //根据homeId完善观察者记录：end_time、duration等
        List<WatchEntity> watchers = watchDao.selectList(new QueryWrapper<WatchEntity>().eq("play_id", homeId));
        LocalDateTime now = LocalDateTime.now();
        watchers.stream().forEach(item -> {
            item.setEndTime(now);
            item.setDuration(ChronoUnit.SECONDS.between(item.getStartTime(), item.getEndTime()));
            watchDao.updateById(item);
        });

        //更新home直播室endAt字段值
        homeEntity.setEndAt(now);
        baseMapper.updateById(homeEntity);
    }
}
